<?php
/**
 * Created by PhpStorm.
 * User: smilezheng
 * Date: 2019/9/9
 * Time: 15:50
 */

namespace App\Controller;

use App\Service\SaveInfoService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class SaveInfoController
 * @package App\Controller
 * @Route("/saveInfo/")
 */
class SaveInfoController
{
    /**
     * @var SaveInfoService
     */
    private $saveInfoService;

    /**
     * SaveInfoController constructor.
     *
     * @param SaveInfoService $saveInfoService
     */
    public function __construct(SaveInfoService $saveInfoService)
    {
        $this->saveInfoService = $saveInfoService;
    }

    /**
     * @param         $query
     * @param         $content
     * @param Request $request
     *
     * @return int
     * @Route("saveValues")
     */
    public function saveValues($query, $content, Request $request)
    {
        if ($request->getMethod() === 'GET') {
            $key   = $query['key'] ?? null;
            $value = $query['value'] ?? null;
        } else {
            $key   = $content['key'] ?? null;
            $value = $content['value'] ?? null;
        }
        return $this->saveInfoService->saveValues($key, $value);
    }

    /**
     * @param $query
     *
     * @return array|null
     * @Route("getValue",methods={"GET"})
     */
    public function getValue($query)
    {
        $key = $query['key'] ?? null;
        $id  = $query['id'] ?? null;
        if ($key !== null) {
            return $this->saveInfoService->getValueByKey($key);
        }
        if ($id !== null) {
            return $this->saveInfoService->getValueById($id);
        }
        return null;
    }
}